//
//  MYAccountManager.m
//  JQLive
//
//  Created by Bruce on 2020/1/15.
//  Copyright © 2020 mlqk. All rights reserved.
//

#import "MYAccountManager.h"
/*!
 *  账户信息路径
 */
#define AccountFilepath [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"account.data"]

@interface MYAccountManager ()

@end
@implementation MYAccountManager

//设置单例
+ (MYAccountManager *)sharedManager
{
    static MYAccountManager *accountManager = nil;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        accountManager = [[MYAccountManager alloc] init];
    });
    return accountManager;
}
/**
 存储账户信息
 
 @param account 账户model
 */
- (void)saveAccount:(MYUserModel *)account
{
    if (!account.accessToken) {
        account.accessToken = self.accountModel.accessToken;
    }
    if (!account.vodToken) {
        account.vodToken = self.accountModel.vodToken;
    }
    self.accountModel = account;
    if ([NSKeyedArchiver archiveRootObject:account toFile:AccountFilepath]) {
        NSLog(@"\n\n\n归档成功：路径%@",AccountFilepath);
    }
    else
    {
        NSLog(@"归档失败");
    }
   
}
/**
 清除账户信息
 */
- (void)clearAccount
{
    self.accountModel = nil;
    
    if ([[NSFileManager defaultManager] removeItemAtPath:AccountFilepath error:nil]) {
        NSLog(@"\n\n\n清除归档成功：路径%@",AccountFilepath);
    }
    else
    {
        NSLog(@"清除归档失败");
    }
}


/**
 外部调用账户
 
 @return 账户model
 */
- (MYUserModel *)account
{
 
    return self.accountModel;
}

- (MYUserModel *)accountModel
{
    if (!_accountModel) {
//        id ss = [NSKeyedUnarchiver unarchiveObjectWithFile:AccountFilepath];
        _accountModel = [NSKeyedUnarchiver unarchiveObjectWithFile:AccountFilepath];

        if (_accountModel) {
               NSLog(@"\n\n\n获取成功：路径%@",AccountFilepath);
           }
           else
           {
               NSLog(@"获取归档失败");
           }
    }
    return _accountModel;
}

- (NSString *)memberId
{
    if (self.accountModel) {
//        DLog(@"accessToken :%@",self.accountModel.accessTokenString);
        return [NSString stringWithFormat:@"%ld",(long)self.accountModel.memberId];
    }
    return @"";
}
@end
